class SuperadminController < ApplicationController
  layout "superadmin"
  
  before_filter :login_required
  require_role "superadmin"
    
  def index
    
  end
  
  def user_list
    @users = User.find(:all)
  end
  
  def region_list
    @regions = Region.find(:all)
     
    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @Regions }
    end
  end
  
  def region_description
    @region = Region.find(params[:id])
    
    respond_to do |format|
      format.html # new.html.erb
    end
  end
  
  def region_description_add
    @region_description = RegionDescription.new(params[:region_description])
    @region_description.region_id = params[:region_id]
    @region_description.description = @region_description.description.strip!
    
    respond_to do |format|
      if @region_description.save
        format.js { render :update do |page|
          page.insert_html :bottom, "region_description_table", {:partial => "superadmin/region_description_row", :object => @region_description}
          page.visual_effect :highlight, "region_description_table", :duration => 2
          page[:region_description_description].value = ''
          page[:region_description_language].value = ''
        end }

      else
        #error
        format.js { render :text => 'error'}
      end
    end
  end
  
  def region_description_update
    @region_description = RegionDescription.find(params[:id])

    respond_to do |format|
      if @region_description.update_attributes(params[:region_description])
        format.js { render :update do |page|
          page.visual_effect :highlight, 'description_' + @region_description.id.to_s
        end }
      else
        format.js { render :update do |page|
  
        end }
      end
    end
  end
  
  def region_description_delete
    @region_description = RegionDescription.find(params[:id])
    if @region_description.destroy
      respond_to do |format|
        format.xml  { head :ok }
        format.js { render :update do |page|
          page.remove "description_#{params[:id]}_row"
          page.visual_effect :highlight, 'region_description_table', :duration => 2
        end }
      end
    else
      respond_to do |format|
        format.js { render :update do |page|
          
        end }
      end
    end
  end

  def region_update
    @region = Region.find(params[:region][:id])
    respond_to do |format|
      if @region.update_attributes(params[:region])
        flash[:notice] = 'Region was successfully updated.'
        format.html { redirect_to :action => 'region_list' }
        format.xml  { head :ok }
      else
        format.html { render :action => "region_edit" }
        format.xml  { render :xml => @artist.errors, :status => :unprocessable_entity }
      end
    end
  end
  
  def region_media  
    @region = Region.find(params[:id])
  end

  def add_role
    user = User.find(params[:user_id])
    role = Role.find_by_name(params[:role_name])
    
    user.add_role(role)
    redirect_to :back
  end
  
  def remove_role
    user = User.find(params[:user_id])
    role = Role.find_by_name(params[:role_name])
    
    user.remove_role(role)
    redirect_to :back
  end
  
  def admin_list
    @admins = User.get_all :admins
  end
  
  def add # called from new artwork steps and edit artist; ajax
   case params[:media][:media_type].to_i
      when Media::TYPE_PICTURE
        @media = Picture.new(params[:media])
      when Media::TYPE_AUDIO
        @media = Audio.new(params[:media])
      when Media::TYPE_VIDEO
         @media = Video.new(params[:media])
     else
       puts params.inspect
       raise 
    end
 
   @media.region_id = params[:region_id]
    
     
    @media.caption = params[:caption]
   
    respond_to do |format|
      if @media.save
        #format.html { render :action => "edit" }
        #format.html { redirect_to edit_admin_artist_path(@media.artist_id) + '#media' }
        format.html { redirect_to :back }
      else
        # TODO: error
        flash[:error] = 'Error: Media NOT uploaded!'
        format.html { redirect_to :back }
      end
    end 
  end
  
  def destroy
    @media = Media.find(params[:id])
    @media.destroy
    @media.artwork.unpublish_if_necessary if @media.artwork
    @media.artist.unpublish_if_necessary if @media.artist
    
    respond_to do |format|
      format.html { redirect_to(medias_url) }
      format.xml  { head :ok }
      format.js { render :update do |page|
        page.remove "media_#{params[:id]}_a"
        page.remove "media_#{params[:id]}_b"
        page.visual_effect :highlight, 'media_table', :duration => 2
      end }
    end
  end
end